package cn.com.controller.frontController;

import cn.com.Enum.SessionAttr;
import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Address;
import cn.com.db.service.AddressService;
import cn.com.db.service.UserService;
import cn.com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Auth(UserAuth.USER)
@RestController
@RequestMapping("front/address")
public class AddressController {

    @Autowired
    AddressService addressService;
    @Autowired
    UserService userService;

    @RequestMapping(path = "/create",method = RequestMethod.POST)
    public String create(HttpServletRequest request,
                           @RequestParam("name")String name,
                           @RequestParam("phone")String phone,
                           @RequestParam("province")String province,
                           @RequestParam("city")String city,
                           @RequestParam("district")String district,
                           @RequestParam("detail")String detail,
                           @RequestParam("def")Boolean def)
    {
        DataResult dataResult=new DataResult();
        String userAuthingId=(String)SessionUtil.getAttr(request,SessionAttr.USERAUTHINGID);
        Address address=new Address();
        address.setId(UidUtil.createUUID());
        address.setName(name);
        address.setPhone(phone);
        address.setProvince(province);
        address.setCity(city);
        address.setDistrict(district);
        address.setDetail(detail);
        address.setCreated_at(TimeUtil.getNowTimestamp());
        address.setUpdated_at(TimeUtil.getNowTimestamp());
        address.setDef(def);
        address.setUser(userAuthingId);
        addressService.createAddress(address);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("创建地址成功");
        dataResult.setData(address);
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(path = "/{id}",method = RequestMethod.GET)
    public String viewAddress(HttpServletRequest request,
                              @PathVariable("id")String id)
    {
        DataResult dataResult=new DataResult();
        Address address=addressService.getAddressById(id);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        dataResult.setData(address);
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(path = "/default",method = RequestMethod.GET)
    public String getUserDefaultAddress(HttpServletRequest request)
    {
        DataResult dataResult=new DataResult();
        String userAuthingId=(String)SessionUtil.getAttr(request,SessionAttr.USERAUTHINGID);
        Address address=addressService.getUserDefAddress(userAuthingId);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        dataResult.setData(address);
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String getUserAddress(HttpServletRequest request,
                                 @RequestParam("page")Integer page,
                                 @RequestParam("count")Integer count)
    {
        DataResult dataResult=new DataResult(page,count);
        String userAuthingId=(String)SessionUtil.getAttr(request,SessionAttr.USERAUTHINGID);
        List<Address> list=addressService.getUserAddress(userAuthingId,page,count);
        Integer total=addressService.countUserAddress(userAuthingId);
        dataResult.setData(list);
        dataResult.setTotal(total);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(path = "/update",method = RequestMethod.POST)
    public String updateAddress(HttpServletRequest request,
                                Address address)
    {
        DataResult dataResult=new DataResult();
        addressService.updateAddress(address);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        dataResult.setData(address);
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    public String deleteAddress(HttpServletRequest request,
                                @RequestParam("id")String id)
    {
        DataResult dataResult=new DataResult();
        addressService.deleteAddress(id);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }



}
